package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //查询语句
        Page<SetmealVO> page = setmealMapper.getSetmeal(setmealPageQueryDTO);
        //封装结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //保存套餐信息
        setmealMapper.save(setmeal);
        //保存setmeal_dish表
        Long setmealId = setmeal.getId();

        SetmealDish setmealDish = new SetmealDish();
        List<SetmealDish> setmealDishlist = setmealDTO.getSetmealDishes();

        for (SetmealDish setmealdish : setmealDishlist) {
            setmealdish.setSetmealId(setmealId);
            BeanUtils.copyProperties(setmealdish,setmealDish);
            setmealDishMapper.save(setmealdish);
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //先删除setmealdish
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealdish : setmealDishList) {
            Long setmealId = setmealdish.getSetmealId();
            setmealDishMapper.deleteById(setmealId);
        }
        //再从DTO中拿setmealdish数据再添加进去
        Long setmealId = setmealDTO.getId();
        List<SetmealDish> setmealDishlist = setmealDTO.getSetmealDishes();

        SetmealDish setmealDish = new SetmealDish();
        for (SetmealDish setmealdish : setmealDishlist) {
            setmealdish.setSetmealId(setmealId);
            BeanUtils.copyProperties(setmealdish,setmealDish);
            setmealDishMapper.save(setmealdish);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     */
    @Override
    public SetmealVO getById(Long id) {
        //先查找出套餐信息  赋值给VO
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //再查出setmeal_dish  赋值给VO
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 套餐启售停售
     * @param status
     * @param id
     */
    @Override
    public void startorstop(Integer status, Long id) {
        //TODO 启售套餐时，先查看套餐内是否有停售菜品，有停售的需要提示信息
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐---在售状态不可以删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断当前套餐是否处于启售状态
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐，并且删除setmeal_dish表中的关联信息
        setmealMapper.delete(ids);
        for (Long id : ids) {
            setmealDishMapper.deleteById(id);
        }

    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
